home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ImagePanel.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  3.4 KB  |  167 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Panel;
  4. import java.net.URL;
  5. import java.awt.Image;
  6. import java.awt.MediaTracker;
  7. import java.awt.Graphics;
  8. import java.awt.Dimension;
  9.  
  10. //     03/02/97    RKM    Changed call to invalidate to repaint
  11.  
  12. public class ImagePanel extends java.awt.Panel
  13. {
  14.     public static final int IMAGE_TILED = 0;
  15.     public static final int IMAGE_CENTERED = 1;
  16.     public static final int IMAGE_SCALED_TO_FIT = 2;
  17.     
  18.     public ImagePanel()
  19.     {
  20.         imageURL = null;
  21.         image = null;
  22.         imageStyle = IMAGE_TILED;
  23.     }
  24.     
  25.     // Properties
  26.     
  27.     /**
  28.      * Paints this component using the given graphics context.
  29.      * This is a standard Java AWT method which typically gets called
  30.      * by the AWT to handle painting this component. It paints this component
  31.      * using the given graphics context. The graphics context clipping region
  32.      * is set to the bounding rectangle of this component and its <0,0>
  33.      * coordinate is this component's top-left corner.
  34.      * 
  35.      * @param g the graphics context used for painting
  36.      * @see java.awt.Component#repaint
  37.      * @see java.awt.Component#update
  38.      */
  39.     public void paint(Graphics g)
  40.     {
  41.         super.paint(g);
  42.         
  43.         if (image != null)
  44.         {
  45.             Dimension dim = size();
  46.             
  47.             int imageWidth = image.getWidth(this);
  48.             int imageHeight = image.getHeight(this);
  49.             
  50.             switch(imageStyle)
  51.             {
  52.                 default:
  53.                 case IMAGE_TILED:
  54.                 {
  55.                     //Calculate number of images that should be drawn horizontally
  56.                     int numHImages = dim.width / imageWidth;
  57.                     
  58.                     //Don't forget remainders
  59.                     if (dim.width % imageWidth != 0)
  60.                         numHImages++;
  61.                     
  62.                     //Calculate number of images that should be drawn vertically
  63.                     int numVImages = dim.height / imageHeight;
  64.                     
  65.                     //Don't forget remainders
  66.                     if (dim.height % imageHeight != 0)
  67.                         numVImages++;
  68.                     
  69.                     int h;
  70.                     int v = 0;
  71.                     for (int vCount = 0;vCount < numVImages;vCount++)
  72.                     {
  73.                         h = 0;
  74.                         for (int hCount = 0;hCount < numHImages;hCount++)
  75.                         {
  76.                             g.drawImage(image, h, v, imageWidth, imageHeight, this);
  77.                             
  78.                             //Increment to next column
  79.                             h += imageWidth;
  80.                         }
  81.                         
  82.                         //Increment to next row
  83.                         v += imageHeight;
  84.                     }
  85.                     
  86.                     break;
  87.                 }
  88.                 
  89.                 case IMAGE_CENTERED:
  90.                 {
  91.                     g.drawImage
  92.                         (image,
  93.                          (dim.width - imageWidth) / 2,
  94.                          (dim.height - imageHeight) / 2,
  95.                          imageWidth,
  96.                          imageHeight,
  97.                          this);
  98.                     
  99.                     break;
  100.                 }
  101.                 
  102.                 case IMAGE_SCALED_TO_FIT:
  103.                 {
  104.                     g.drawImage(image, 0, 0, dim.width, dim.height, this);
  105.                     
  106.                     break;
  107.                 }
  108.             }//switch
  109.         }
  110.     }
  111.     
  112.     public void setImageURL(URL url)
  113.     {
  114.         imageURL = url;
  115.         if (imageURL != null)
  116.         {
  117.             image = getToolkit().getImage(imageURL);
  118.             if (image != null)
  119.             {
  120.                 MediaTracker mt = new MediaTracker(this);
  121.                 try
  122.                 {
  123.                     mt.addImage(image, 0);
  124.                     mt.waitForAll();
  125.                 }
  126.                 catch (InterruptedException ie)
  127.                 {
  128.                 }
  129.             }
  130.         }
  131.         
  132.         repaint();
  133.     }
  134.     
  135.     public URL getImageURL()
  136.     {
  137.         return imageURL;
  138.     }
  139.     
  140.     public void setStyle(int newStyle)
  141.     {
  142.         if (newStyle != imageStyle)
  143.         {
  144.             imageStyle = newStyle;
  145.             repaint();
  146.         }
  147.     }
  148.     
  149.     public int getStyle()
  150.     {
  151.         return imageStyle;
  152.     }
  153.     
  154.     // Methods
  155.     
  156.     public Image getImage()
  157.     {
  158.         return image;
  159.     }
  160.     
  161.     // Private members
  162.     
  163.     private Image image;
  164.     private URL imageURL;
  165.     private int imageStyle;
  166. }
  167.